package org.chs.ts.batch.util;



import lombok.extern.slf4j.Slf4j;
import org.chs.ts.base.common.util.DateUtils;
import org.chs.ts.base.constant.CommonConstant;
import org.chs.ts.batch.pojo.BatchTradeCapitalTransReq;
import org.chs.ts.batch.execute.BatchTaskExecute;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Compay 盛唐天行
 * @Author: wuwei
 * @Censor By wuwei
 * @Sence 2022/12/21 13:46
 * @Modify 2022/12/21 13:46
 * @Description
 */
@Component
@Slf4j
public class TaskBuilder {

    @Resource
    private ParameterUtil parameterUtil;


    /**
     * 生成 批量任务执行ID
     */
    public String taskExecId(String batchSysCode,String taskGroup, String taskId, String taskExecDate) {
        //String nextDay = parameterUtil.getParameterByCode(CommonConstant.CURRENT_DAY);
        return batchSysCode +taskExecDate + taskGroup + taskId;
    }

    public String taskExecId(String batchSysCode, String taskGroup, String taskId, String taskStepId, String taskExecDate) {
        //String nextDay = parameterUtil.getParameterByCode(CommonConstant.CURRENT_DAY);
        return batchSysCode + taskExecDate + taskGroup + taskId + taskStepId;
    }

    public String getTaskExecDate() {
        //return parameterUtil.getParameterByCode(CommonConstant.LAST_DAY);
        return CommonUtil.getDayByParameter(parameterUtil.getParameterByCode(CommonConstant.MWBP_TRANS_DAY),-1);

    }



    public String getCutTime(String oldChar, String newChar) {
        return parameterUtil.getParameterByCode(CommonConstant.MWBP_TRANS_DAY).replace(oldChar, newChar);
    }


    public String getTaskExecDate(String taskExecDate, SimpleDateFormat simpleDateFormat) {
        SimpleDateFormat simpleDateFormat_default = new SimpleDateFormat("yyyyMMdd");

        Date date;
        try {
            date = simpleDateFormat_default.parse(taskExecDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return simpleDateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("TaskBuilder -> String转Date日期失败：", e);
            return "";
        }

    }

    public String getTaskExecDateBefore(String taskExecDate, SimpleDateFormat simpleDateFormat) {
        SimpleDateFormat simpleDateFormat_default = new SimpleDateFormat("yyyyMMdd");
        Date date;
        try {
            date = simpleDateFormat_default.parse(taskExecDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            return simpleDateFormat.format(calendar.getTime());
        } catch (ParseException e) {
            //e.printStackTrace();
            log.error("TaskBuilder -> String转Date日期失败：", e);
            return "";
        }

    }



    /**
     * 根据传入参数获取当前时间对应 的时间，不包括时分秒
     * <br>向前1天传负数-1,向后一天传正数1<br/>
     *
     * @return String
     */
    public String getDayBy(int dayDiff) {
        Date date = getCoreDateTimeByCoreDay();
        Date result = DateUtils.addDay(date, dayDiff);
        return DateUtils.getDate8Str(result);
    }

    /**
     * 获取中间业务系统业务期
     *
     * @return CURRENT_DAY 中间业务业务期与核心一致
     */
    public String getCurentDay() {
        return parameterUtil.getParameterByCode(CommonConstant.MWBP_TRANS_DAY);
    }

    /**
     * 获取中间业务系统核心日期
     *
     * @return CORE_DAY 中间业务系统核心日期
     */
    public String getCoreDay() {
        return parameterUtil.getParameterByCode(CommonConstant.MWBP_TRANS_DAY);
    }

    /**
     * 获取中间业务系统核心日期 Date String:  日切日期 + 当前时间  yyyy-MM-dd HH:mm:ss
     *
     * @return String
     */
    public String getCoreDateTimeStrByCoreDay() {
        String date = parameterUtil.getParameterByCode(CommonConstant.MWBP_TRANS_DAY);
        String newDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
        return newDate + DateUtils.getTimeAfterDate();
    }

    /**
     *
     * @return Date
     */
    public Date getCoreDateTimeByCoreDay() {
        return DateUtils.getDateTimeFormat(getCoreDateTimeStrByCoreDay());
    }



    public BatchTradeCapitalTransReq initBatchTradeCapitalTransReq(BatchTaskExecute batchServiceBean, boolean noTaskParams) {

        BatchTradeCapitalTransReq request = new BatchTradeCapitalTransReq();

        String cutTime = this.getCutTime(":", "");
        String startExecDate = this.getTaskExecDateBefore(batchServiceBean.getTaskObject().getExecDate(), new SimpleDateFormat("yyyyMMdd")); //交易日
        String endExecDate = this.getTaskExecDate(batchServiceBean.getTaskObject().getExecDate(), new SimpleDateFormat("yyyyMMdd"));

        if (!noTaskParams) {
            // 外部参数支持
            // protocol_no '行业代码 D4430  电力生产 ：D4410 燃气生产和供应业 ：D4500 自来水生产和供应 ：D4610[protocol_no]',
            request.setProtocolNo(batchServiceBean.getParamsObjByKey("protocol_no"));
            // company_cust_code , -- 单位编码
            request.setCompanyCustCode(batchServiceBean.getParamsObjByKey("company_cust_code"));
        }


        request.setCurrentTransDate(endExecDate);
        request.setLastTransDate(startExecDate);
        request.setTransTime(cutTime);
        //request.setTransState(CAPITAL_DEAL_STATUS);

        return request;
    }

    public BatchTradeCapitalTransReq initBatchTradeCapitalTransReq(BatchTaskExecute batchServiceBean,String channelType, boolean noTaskParams) {

        BatchTradeCapitalTransReq request = initBatchTradeCapitalTransReq(batchServiceBean, noTaskParams);
        request.setChannelType(channelType);
        return request;
    }


}
